package com.wswy.wzcx.module;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.WorkerThread;
import android.text.TextUtils;
import android.widget.Toast;
import com.wswy.wzcx.statistics.StatTools;
import com.wswy.wzcx.statistics.StatisticsId;
import com.wswy.wzcx.ui.Constants;
import timber.log.Timber;

public class WReceiver extends BroadcastReceiver {

  private static final String TAG = "WReceiver";

  public static String cookieRef = null;

  @WorkerThread
  public static String getCookie(){

    synchronized (WReceiver.class){

      try {
        WReceiver.class.wait(1000*10);
      } catch (Throwable e) {
        e.printStackTrace();
      }

      return cookieRef;
    }
  }

  public static void reset(){
    cookieRef = null;
  }


  @Override
  public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    Timber.e("onReceive --> "+action);

    if (TextUtils.equals(Constants.ACTION_WEB_GW122_END,action)){

      //官网122
      String cookie = intent.getStringExtra("cookie");
      String url = intent.getStringExtra("url");

      Timber.e("onReceive --> "+url);
      Timber.e( "onReceive --> "+cookie);


      cookieRef = cookie;
      synchronized (WReceiver.class){
        WReceiver.class.notify();
      }

    }else if (TextUtils.equals(Constants.ACTION_BG_WORK_END,action)){
      //后台任务

      //执行下一个

      if (GlobalConfigManager.getInstance().getDebugConfig().openLog){
        String url = intent.getStringExtra("url");
        int time = intent.getIntExtra("time",0);
        Toast.makeText(context,"bg time:"+time+" url:"+url,Toast.LENGTH_LONG).show();
      }
      StatTools.sendShow(context, StatisticsId.bg_fake_web_success);
    }



  }
}
